﻿Ext.onReady(function () {
    PageSafe();

    var modelstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'GetModelList'),
        root: 'ResponseObject',
        fields: ['PID', 'ModelName', 'ModelCode'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var modelsm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = equipmentgrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentEquipment = k[0].json;
                    gystore.load();
                }
            }
        }
    });
    var modelgrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        height: 550,
        sm: modelsm,
        loadMask: true, //加载
        columns: [
       modelsm,
       { id: 'ModelName', header: '模板名称', width: 100, dataIndex: 'ModelName', sortable: true },
       { id: 'ModelCode', header: '模板代码', width: 300, dataIndex: 'ModelCode', sortable: true }
   ],
        store: modelstore
    });
    modelstore.load();

    var page = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'center',
            title: '广告模版管理',
            iconCls: 'modelwin-icon',
            tbar: [{ text: '增加模版', iconCls: 'add-icon', handler: function () {
                var addmodelwin = new Ext.Window({
                    height: 200,
                    width: 300,
                    title: '增加模版',
                    iconCls: 'areawin-icon',
                    resizable: false,
                    modal: true,
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '模板名称',
                        width: 100,
                        id: 'ModelName',
                        name: 'ModelName'
                    }, {
                        fieldLabel: '模板代码',
                        width: 100,
                        id: 'ModelCode',
                        name: 'ModelCode'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {

                        var re = new JsonRequest();
                        re.params.model = {
                            ModelName: Ext.getCmp("ModelName").getValue(),
                            ModelCode: Ext.getCmp("ModelCode").getValue()
                        };
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'AddModel'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("操作提示", "广告模板新增成功");
                                    modelstore.load();
                                }
                                else {
                                    Ext.Msg.alert("操作提示", "广告模板新增失败，请联系管理员");
                                }
                                addmodelwin.close();
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { addmodelwin.close(); } }]
                }).show();
            } 
            }, { text: '修改模版', iconCls: 'update-icon' }, { text: '删除模版', iconCls: 'del-icon'}],
            items: modelgrid
        }]
    });
});